package com.yishusmt.wechatapi.wechatdev.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.yishusmt.wechatapi.wechatdev.dto.base.Result;
import com.yishusmt.wechatapi.wechatdev.utils.AliyunSmsUtil;
import com.yishusmt.wechatapi.wechatdev.utils.RandomUtil;
import com.yishusmt.wechatapi.wechatdev.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhangyi
 * @date 2018/9/11 14:26
 */
@RestController
@RequestMapping("/code")
@Api(description = "验证码接口")
public class CodeController extends BaseController {


    @Resource
    private RedisUtil redisUtil;

    /**
     * 发送验证码
     *
     * @param openId
     * @return
     */
    @PostMapping("/send")
    @ApiOperation(value = "发送验证码，需要传入OpenId",httpMethod = "POST")
    public Result send(String openId,String phone){
        String s = RandomUtil.randomCode(4);
        Object o = redisUtil.get(openId);
        if(o == null) {
            SendSmsResponse sendSmsResponse = AliyunSmsUtil.sendCodeSms(phone, s);
                //存入Redis
                boolean set = redisUtil.set(openId, s, 60 * 3L);
                return set ? new Result("发送成功") : new Result("发送失败");
        }else {
            return new Result("信息已经发送，请勿重复点击！");
        }
    }

}
